<?php
declare (strict_types = 1);

namespace app\model;

use think\facade\Db;
use think\Model;

/**
 * @mixin \think\Model
 */
class Project1s extends Model
{
    protected $table = "admin_user";

    public function getAll($admin_id){
        if($admin_id == 1){
            $data = $this->table('admin_power')->select()->toArray();
            $data = self::getList($data,0);
            foreach($data as $key=>$value){
                $arr = $this->table('admin_power')->where("f_id=".$value['f_id'])->select()->toArray();
                $data[$key]['son'] = $arr;
            }
        }else if($admin_id >= 2 && $admin_id <= 5){
            $data = Db::table('admin_user')
                ->alias('u')
                ->join('admin_user_role ur','u.u_id = ur.u_id')
                ->join('admin_role r','ur.r_id = r.r_id')
                ->join("admin_role_power rp",'r.r_id = rp.r_id')
                ->join("admin_power p",'rp.p_id = p.p_id')
                ->where('u.u_id',"$admin_id")
                ->select()->toArray();
            $data = self::getList($data,1);
            foreach($data as $key=>$value){
                $arr = $this->table('admin_power')->where("f_id=".$value['f_id'])->select()->toArray();
                $data[$key]['son'] = $arr;
            }
        }else{
            $data = Db::table('admin_user')
                ->alias('u')
                ->join('admin_user_role ur','u.u_id = ur.u_id')
                ->join('admin_role r','ur.r_id = r.r_id')
                ->join("admin_role_power rp",'r.r_id = rp.r_id')
                ->join("admin_power p",'rp.p_id = p.p_id')
                ->where('u.u_id',"$admin_id")
                ->select()->toArray();
        }
        return $data;
    }

    static $datalist;
    function getList($data,$parend_id){
        foreach($data as $key=>$value){
            if($value['f_id'] == $parend_id){
                self::$datalist["$key"] = $value;
                self::getList($data,$value['p_id']);
            }
        }
        return self::$datalist;
    }
}